package com.cc2019.buke.user.frag;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.fragment.app.Fragment;


import com.cc2019.buke.R;
import com.cc2019.buke.user.Activity.MyInfoActivity;
import com.cc2019.buke.user.Activity.ShoucangActivity;
import com.cc2019.buke.user.Activity.address.AddressActivity;
import com.cc2019.buke.user.Activity.help.HelpActivity;
import com.cc2019.buke.user.Activity.LoginActivity;
import com.cc2019.buke.user.Activity.SettingActivity;


public class UserFragment extends Fragment {
    private View view;
    Button button;
    private String name ;
    private Button settingButton;
    private Button addressButton;
    private Button helpButton;
    private Button shoucangButton;

    private Button changedark;
    private Button changeday;

    public UserFragment(){}
    public UserFragment(String name) {
        this.name = name;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_user, container, false);
        button =(Button)view.findViewById(R.id.to_login);
        settingButton = (Button) view.findViewById(R.id.setting_Fragment);
        addressButton=(Button) view.findViewById(R.id.addresss);
        helpButton=(Button) view.findViewById(R.id.help_fragment);
        shoucangButton=(Button) view.findViewById(R.id.shoucang_fragment);

        changedark=(Button) view.findViewById(R.id.night);
        changeday=(Button) view.findViewById(R.id.daytime);

        if (name !=null){
            settingButton.setVisibility(View.VISIBLE);
            button.setText(name);
            addressButton.setVisibility(View.VISIBLE);
            button.setText(name);
            helpButton.setVisibility(View.VISIBLE);
            button.setText(name);
            shoucangButton.setVisibility(View.VISIBLE);
            button.setText(name);
            changedark.setVisibility(View.VISIBLE);
            button.setText(name);
            changeday.setVisibility(View.VISIBLE);
            button.setText(name);
        }else {
            settingButton.setVisibility(View.VISIBLE);
            addressButton.setVisibility(View.VISIBLE);
            helpButton.setVisibility(View.VISIBLE);
            shoucangButton.setVisibility(View.VISIBLE);
            changedark.setVisibility(View.VISIBLE);
            changeday.setVisibility(View.VISIBLE);
        }
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        button = getActivity().findViewById(R.id.to_login);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (name==null){
                    Intent intent = new Intent(getActivity(), LoginActivity.class);
                    startActivity(intent);
                    getActivity().finish();
                }

            }

        });
        settingButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(name!=null){
                    //传递姓名
                    Intent intent=new Intent(getActivity(), SettingActivity.class);
                    intent.putExtra("username",name);
                    Log.e("TAG", "onClick: "+name );
                    startActivityForResult(intent,1);
                }else {
                    Intent intent=new Intent(getActivity(),LoginActivity.class);
                    Toast.makeText(getContext(), "请先登录~", Toast.LENGTH_SHORT).show();
                    startActivity(intent);
                    getActivity().finish();
                }
            }
        });

        addressButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(name!=null){
                    Intent intent=new Intent(getActivity(), AddressActivity.class);
                    startActivityForResult(intent,1);
                }else {
                    Intent intent=new Intent(getActivity(),LoginActivity.class);
                    Toast.makeText(getContext(), "请先登录~", Toast.LENGTH_SHORT).show();
                    startActivity(intent);
                    getActivity().finish();
                }
            }
        });

        helpButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getActivity(), HelpActivity.class);
                startActivityForResult(intent,1);
            }
        });

        shoucangButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(name!=null){
                Intent intent=new Intent(getActivity(), ShoucangActivity.class);
                startActivityForResult(intent,1);
            }else {
                    Intent intent=new Intent(getActivity(),LoginActivity.class);
                    Toast.makeText(getContext(), "请先登录~", Toast.LENGTH_SHORT).show();
                    startActivity(intent);
                    getActivity().finish();
                }
            }
        });

        changeday.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((AppCompatActivity) getActivity()).getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_NO);
                (getActivity()).recreate();
            }
        });
        changedark.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((AppCompatActivity) getActivity()).getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_YES);
                (getActivity()).recreate();

            }
        });

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if(requestCode==1&&resultCode==10){
//            SettingActivity settingActivity=(SettingActivity) getActivity();
//            FragmentManager fragmentManager=settingActivity.getSupportFragmentManager();
//            FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
//            fragmentTransaction.replace(R.id.login_out,new UserFragment(null));
//            fragmentTransaction.commit();
            name = null;
            button.setText("登录/注册");
            settingButton.setVisibility(View.VISIBLE);
            addressButton.setVisibility(View.VISIBLE);
            helpButton.setVisibility(View.VISIBLE);
            shoucangButton.setVisibility(View.VISIBLE);

            changedark.setVisibility(View.VISIBLE);
            changeday.setVisibility(View.VISIBLE);

        }
    }
}